<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"
	xmlns="http://www.w3.org/2005/Atom">
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

	<xsl:param name="section"/>
	<xsl:param name="exist-url"/>
	<xsl:param name="url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//url"/>
	</xsl:param>
	<xsl:param name="eadheader_url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//eadheader_url"/>
	</xsl:param>
	<xsl:param name="raw-page"/>
	<xsl:variable name="page">
		<xsl:choose>
			<xsl:when test="number($raw-page)">
				<xsl:value-of select="$raw-page"/>
			</xsl:when>
			<xsl:otherwise>1</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>


	<xsl:template match="/">
		<xsl:variable name="start" select="(25 * $page) - 24"/>
		<xsl:variable name="end" select="25 * $page"/>
		<xsl:variable name="numFound">
			<xsl:value-of select="number(//result[@name='response']/@numFound)"/>
		</xsl:variable>
		<xsl:variable name="total" select="ceiling($numFound div 25)"/>

		<feed xmlns="http://www.w3.org/2005/Atom">
			<title>
				<xsl:value-of
					select="document(concat($exist-url, '/site_info.xml'))/site_info/title"/>
			</title>
			<xsl:if test="not($section = 'display')">
				<link href="{$url}feed/" rel="self"/>
			</xsl:if>
			<link href="{$url}"/>
			<id><xsl:value-of select="document($eadheader_url)//eadid"/></id>
			<xsl:if test="not($section = 'display')">
				<xsl:if test="not($page = $total)">
					<link rel="next" href="{$url}feed/page/{$page + 1}/"/>
				</xsl:if>
				<link rel="last" href="{$url}feed/page/{$total}/"/>
			</xsl:if>
			<author>
				<name><xsl:value-of select="document($eadheader_url)//publisher"/></name>
			</author>

			<xsl:apply-templates
				select="//doc[position() &gt;= $start and position() &lt;= $end]"/>
		</feed>
	</xsl:template>

	<xsl:template match="doc">
		<entry>
			<title>
				<xsl:value-of select="str[@name='unittitle_display']"/>
			</title>
			<link href="{$url}display/{str[@name='source_meta']}/{str[@name='id']}"/>
			<link rel="alternate" type="text/xml" href="{$url}display/{str[@name='source_meta']}/{str[@name='id']}.xml"/>
			<link rel="alternate" type="application/atom+xml"
				href="{$url}display/{str[@name='source_meta']}/{str[@name='id']}.atom"/>
			<link rel="alternate" type="application/rdf+xml"
				href="{$url}display/{str[@name='source_meta']}/{str[@name='id']}.rdf"/>
			<id>
				<xsl:value-of select="document($eadheader_url)//eadid"/><xsl:text>/</xsl:text>
				<xsl:value-of select="str[@name='id']"/>
			</id>
			<xsl:if test="str[@name='origination_display']">
				<creator>
					<name>
						<xsl:value-of select="str[@name='origination_display']"/>
					</name>
				</creator>
			</xsl:if>
			<updated>
				<xsl:value-of select="date[@name='timestamp']"/>
			</updated>
		</entry>
	</xsl:template>
</xsl:stylesheet>
